import datetime
import os
import sys

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager


# 创建一个浏览器对象
class Driver:
    driver = ""

    def __init__(self):
        options = webdriver.EdgeOptions()
        # options.add_argument("--headless")
        self.driver = webdriver.Edge(service=Service(EdgeChromiumDriverManager().install()), options=options)

    # 创建屏幕截图
    def get_screenshot(self):
        # 图片文件名称: 年-月-日-时分秒.png
        # 图片路径: ../images/2025-06-02/图片文件
        dirname = "../images/" + datetime.datetime.now().strftime("%Y-%m-%d")
        if not os.path.exists(dirname):
            os.makedirs(dirname)
        # 图片文件名称，函数名-具体时间
        filename = (sys._getframe().f_back.f_code.co_name + "-" +
                    datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png")
        self.driver.save_screenshot(dirname + "/" + filename)

    def quit(self):
        self.driver.quit()


forum_driver = Driver()
